package de.teamgamma.cansat.groundstation.export;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;

import de.szut.gui.language.Languages;
import de.szut.gui.language.TranslatableComponent;
import de.szut.gui.language.TranslatableStyledButton;
import de.teamgamma.cansat.groundstation.control.Controller;
import de.teamgamma.cansat.groundstation.main.DefaultPanel;
import de.teamgamma.cansat.groundstation.options.OptionKeys;

/**
 * Panel for viewing the logged pictures
 * @author Marc Huisinga
 *
 */
public class PicViewerPanel extends DefaultPanel {
	private static final long serialVersionUID = 8870281645876583753L;
	
	public static final int PIC_SWITCH_START_DELAY = 0;
	public static final int PIC_SWITCH_DELAY = 40;
	
	private int picIndex;
	private JLabel lastPicLabel;
	private String[] pics;
	private Timer picTimer;
	private TimerTask picTask;
	private long timePerPic;
	
	private boolean paused;
	private TranslatableStyledButton btnPause;
	
	private ArrayList<TranslatableComponent> translatableComponents;
	
	public PicViewerPanel(String[] files) {
		translatableComponents = new ArrayList<TranslatableComponent>();
		this.pics = files;
		paused = false;
		picIndex = 0;
		
		if (Controller.getInstance().getOptions().getOptionValue(OptionKeys.DEBUGMODE).equals("0")) {
			if (files.length > 1) {
				timePerPic = (new File(files[files.length-1]).lastModified() - new File(files[0]).lastModified()) / files.length;
			} else {
				timePerPic = PIC_SWITCH_DELAY;
			}
		} else {
			timePerPic = PIC_SWITCH_DELAY;
		}
		this.getHeader().setVisible(false);
		getReturnButton().addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				picTask.cancel();
				picTimer.cancel();
				picTimer.purge();
				Controller.getInstance().getGuiController().toggleCurrentPanel(new ExportPanel());
			}
		});
		Languages.prepareComponent("export.PicViewerPanel.return", getReturnButton(), translatableComponents);
		getReturnButton().setLocation(getReturnButton().getX(), getReturnButton().getY());
		
		btnPause = new TranslatableStyledButton();
		btnPause.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (paused) {
					picTask = buildPicTask();
					picTimer.scheduleAtFixedRate(picTask, PIC_SWITCH_START_DELAY, timePerPic);
					btnPause.setTranslationKey("export.PicViewerPanel.pause");
					Controller.getInstance().getGuiController().getLanguages().translateSpecificComponent(btnPause);
					paused = false;
				} else {
					picTask.cancel();
					picTimer.cancel();
					picTimer = new Timer();
					btnPause.setTranslationKey("export.PicViewerPanel.unpause");
					Controller.getInstance().getGuiController().getLanguages().translateSpecificComponent(btnPause);
					paused = true;
				}
			}
		});
		Languages.prepareComponent("export.PicViewerPanel.pause", btnPause, translatableComponents);
		btnPause.setBounds(getReturnButton().getX()-getReturnButton().getWidth()-10, getReturnButton().getY(), getReturnButton().getWidth(), getReturnButton().getHeight());
		btnPause.setFont(getReturnButton().getFont());
		add(btnPause);
		
		this.setLayout(new BorderLayout(0, 0));
		picTimer = new Timer();
		picTask = buildPicTask();
		picTimer.scheduleAtFixedRate(picTask, PIC_SWITCH_START_DELAY, timePerPic);
		
		Controller.getInstance().getGuiController().getLanguages().translateListOfComponents(translatableComponents);
	}
	
	private TimerTask buildPicTask() {
		TimerTask picTask = new TimerTask() {
			@Override
			public void run() {
				showPic(picIndex);
				if (picIndex == pics.length - 1) {
					picIndex = 0;
				} else {
					picIndex ++;
				}
			}
			
		};
		return picTask;
	}
	
	private void showPic(int picIndex) {
		try {
			File picFile = new File(pics[picIndex]);
			String picFileName = picFile.getName();
			if(picFileName.substring(picFileName.lastIndexOf(".") + 1, picFileName.length()).equals("jpg")) {
				BufferedImage img = ImageIO.read(picFile);
				if (lastPicLabel != null) {
					remove(lastPicLabel);
				}
				JLabel picLabel = new JLabel(new ImageIcon(img));
				add(picLabel);
				Controller.getInstance().getGuiController().getMainFrame().repaint();
				Controller.getInstance().getGuiController().getMainFrame().revalidate();
				lastPicLabel = picLabel;		
			}
		} catch (IOException e) {
			// Pic is being ignored
		}
	}

}
